package com.xiaoerkeji.eshop.ui.dialog;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.xiaoerkeji.eshop.R;
import com.xiaoerkeji.eshop.databinding.DialogCommonBinding;
import com.xiaoerkeji.eshop.listener.MyDialogListener;
import com.lt.base.BaseDialog;
import com.lt.phone.ScreenHelper;
import com.lt.view.ViewHelper;

import java.util.Objects;

/**
 * 通用对话框
 *
 * @author luotian
 */
public class DialogFactory extends BaseDialog
{

    private MyDialogListener listener = null;

    private DialogCommonBinding binding;

    public DialogFactory(Activity activity)
    {
        super(activity, R.style.dialog_my);
        this.mActivity = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        binding = DialogCommonBinding.inflate(getLayoutInflater());
        Objects.requireNonNull(getWindow()).setContentView(binding.getRoot());
        setDialogSize(ScreenHelper.getScreenWidth() - ScreenHelper.getInstance().dip2px(70), 0);
        this.setOnCancelListener(dialog ->
        {
            close();
            if (listener != null)
            {
                listener.onClose();
            }
        });
        binding.viewLeft.setOnClickListener(this::clickEvent);
        binding.tvLeft.setOnClickListener(this::clickEvent);
        binding.viewRight.setOnClickListener(this::clickEvent);
        binding.tvRight.setOnClickListener(this::clickEvent);
    }

    @SuppressLint("NonConstantResourceId")
    private void clickEvent(View v)
    {
        switch (v.getId())
        {
            case R.id.view_left:
            case R.id.tv_left:
                close();
                if (listener != null)
                {
                    listener.onClickLeftButton();
                }
                break;
            case R.id.view_right:
            case R.id.tv_right:
                close();
                if (listener != null)
                {
                    listener.onClickRightButton();
                }
                break;
        }
    }

    /**
     * @param title    标题，可以为null
     * @param content  提示内容，不能为null
     * @param left     为null，隐藏
     * @param right    为null，button text 默认为确定
     * @param gravity  文字内容位置
     * @param listener 左右按钮的监听事件，为null时，默认点击消失
     */
    public DialogFactory show(Object title, Object content, Object left,
                              Object right, int gravity, MyDialogListener listener)
    {
        if (showDialog())
        {
            if (title == null)
            {
                binding.tvTitle.setVisibility(View.GONE);
            } else
            {
                binding.tvTitle.setVisibility(View.VISIBLE);
                ViewHelper.getInstance().setTextView(binding.tvTitle, title);
            }
            ViewHelper.getInstance().setTextView(binding.tvContent, content);
            if (gravity != -1)
            {
                binding.tvContent.setGravity(gravity);
            }
            if (left != null)
            {
                binding.viewLeft.setVisibility(View.VISIBLE);
                ViewHelper.getInstance().setTextView(binding.tvLeft, left);
            }
            if (right != null)
            {
                binding.line.setVisibility(View.VISIBLE);
                binding.viewRight.setVisibility(View.VISIBLE);
                ViewHelper.getInstance().setTextView(binding.tvRight, right);
            } else
            {
                binding.line.setVisibility(View.GONE);
                binding.viewRight.setVisibility(View.GONE);
            }
            this.listener = listener;
        }
        return this;
    }

    public TextView RightTv()
    {
        return binding.tvRight;
    }
}
